//
//  CustomVoucherTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/5.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CustomVoucherTableViewCell.h"
#import "CouponListModel.h"
@interface CustomVoucherTableViewCell()
@property(nonatomic, strong) CouponModel *coupon;
@end

@implementation CustomVoucherTableViewCell

- (void)awakeFromNib {
    self.bgView.layer.cornerRadius=3;
    // Initialization code
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(self.voucherNameLabel.originX+16, self.voucherNameLabel.bottom-5, self.width-26-133-15-28, 20)];
    [self addSubview:imageView1];
    
    
    UIGraphicsBeginImageContext(imageView1.frame.size);   //开始画线
    [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状
    
    
    CGFloat lengths[] = {3,3};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, [UIColor whiteColor].CGColor);
    
    CGContextSetLineDash(line, 0, lengths, 2);  //画虚线
    CGContextMoveToPoint(line, 0.0, 20.0);    //开始画线
    CGContextAddLineToPoint(line, 310.0, 20.0);
    CGContextStrokePath(line);
    
    imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
}

- (void)setEnableSelect:(BOOL)enableSelect {
    _enableSelect = enableSelect;
    if (_enableSelect) {
        self.chooseBtn.hidden = NO;
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
}

- (void)updateCouponInfo:(CouponModel *)model {
    self.coupon = model;
    //self.voucherValue.text = model.discount;
    self.voucherNameLabel.text = model.name;
    self.voucherRuleLabel.text = model.minimum;
    self.voucherDateLabel.text = [NSString stringWithFormat:@"%@-%@", model.date_start, model.date_end];
    if(self.enableSelect) {
        [self.coupon addObserver:self forKeyPath:@"isChecked" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    }
}
-(void)reshCouponState:(CouponModel*)model selected:(NSString*)selectedCode{
    if ([model.code isEqualToString:selectedCode]) {
        self.coupon.isChecked=YES;
    }
}
- (IBAction)chooseCouponAction:(id)sender {
    self.coupon.isChecked = !self.coupon.isChecked;
    if(self.selectCouponBlock) {
        if (self.coupon.isChecked) {
            self.selectCouponBlock(self.coupon);
        } else {
            self.selectCouponBlock(nil);
        }
    }
}

- (void)setBtnImages {
    if (self.coupon.isChecked) {
        [self.chooseBtn setImage:[UIImage imageNamed:@"btn_select_selectd"] forState:UIControlStateNormal];
    } else {
        [self.chooseBtn setImage:[UIImage imageNamed:@"btn_select_nor"] forState:UIControlStateNormal];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"isChecked"]) {
        [self setBtnImages];
    }
}

- (void)dealloc {
    if(self.enableSelect) {
        [self.coupon removeObserver:self forKeyPath:@"isChecked"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
